$(function(){
	//页面加载完之后运行
	setValueToDefault();
	add_school();
	getDataByCurrCity(1);
	registerEvent();
	setOtherConditionToDefalut();
});

/**
 * 加载数据--暂未实现
 * @param {Object} page
 */
function loadData(page){
	
	var submit_buyType=$("#submit_buyType").val();
	var submit_schoolId=$("#submit_schoolId").val();
	var submit_goodsType=$("#submit_goodsType").val();
	var submit_PriceFrom=$("#submit_PriceFrom").val();
	var submit_PriceTo=$("#submit_PriceTo").val();
	
	
	var online_or_all=$("#online_or_all").val();
	var sort_type=$("#sort_type").val();
	var query_by_time=$("#query_by_time").val();
	var query_string_detail=$("#query_string_detail").val();
	
	$("#table_body_info").html("");
	$("#total_info").html("");
	
	$.post(rootPath+'/json/getGoodsByDetail.action',
			{
				buyOrSale:submit_buyType,
				schoolId:submit_schoolId,
				goodsType:submit_goodsType,
				priceFrom:submit_PriceFrom,
				priceTo:submit_PriceTo,
				lookOnlineOnly:(online_or_all=="0"?false:true),
				sortType:sort_type,
				queryDays:query_by_time,
				queryStringdetail:query_string_detail,
				page:page,
				pageSize:20
			},
			function(data){
				//$('<span>&nbsp;&nbsp;&nbsp;&nbsp;统计信息：&nbsp;&nbsp总共'+data.total+'条信息,当前第'+data.page+'页,每页大小'+data.pageSize+'条</span>').appendTo($("#total_info"));
				generateNewButton(data.page,data.pageSize,data.total);
				withPhoto='';
				noPhoto='';
				for(var i=0;i<data.goods.length;i++){
					withPhoto=withPhoto+'<tr><td><img alt="" style="width: 70px;height: 70px;" src="';
					if(data.goods[i].photo1!==null && data.goods[i].photo1!=''){
						withPhoto=withPhoto+''+data.goods[i].photo1;
					}else{
						withPhoto=withPhoto+'image/error.jpg';
					}
					withPhoto=withPhoto+'"<br>标题：'+data.goods[i].title+'<br>('+data.goods[i].supplyordemand+')</td>' +
							  '<td><table>' +
							  '<tr><td>发布时间:'+data.goods[i].publishTime+'</td>' +
							  '<td>更新时间:'+data.goods[i].updateTime+'</td>' +
							  '<td>竞拍类型:'+(data.goods[i].isJingpaiOrNot=='1'?'竞拍商品':'非竞拍商品')+'</td>' +
							  '<td>价格：'+data.goods[i].price+"(元)</td>" +
							  
							  "<a href='acquireGoodsById.action?goodsId="+data.goods[i].id+"'>"+data.goods[i].title+"</a></td>" +
							  "</table></td></tr>";
					noPhoto=noPhoto+'<tr><th class="huya1">发布时间:'+data.goods[i].publishTime+'</th>' +
							'<td>' +
							"<a href='acquireGoodsById.action?goodsId="+data.goods[i].id+"'>"+data.goods[i].title+"</a>&nbsp;&nbsp;" +
							''+(data.goods[i].isJingpaiOrNot=='1'?'竞拍商品':'非竞拍商品')+'&nbsp;&nbsp;' +
							'更新时间:'+data.goods[i].updateTime+'' +
							'价格：'+data.goods[i].price+"(元)" +
							
							"</td></tr>";
					//.appendTo("#table_body_info");
				}
				if($("#picture_or_word").val()=='word'){
					$(noPhoto).appendTo("#table_body_info");
				}else{
					$(withPhoto).appendTo("#table_body_info");
				}
			},
			"json"
	);
}
//查询条件选择--选择交易意图(求购、出售)
function getDataByBuyType(buyType,id1,id2,id3){
	if($("#"+id1).css("font-weight")=="700"){
		return;
	}else{
		change_css_black(id1);
		change_css_normal(id2);
		change_css_normal(id3);
		$("#submit_buyType").val(buyType);
		
		//把关键字搜索输入框置空
		$("#keyworks_input").val("");
		$("#query_string_detail").val("");
		
		setOtherConditionToDefalut();
		loadData(1);
	}
}
//查询条件选择--选择学校
function getDataBySchoolId(id,index){
	if($("#sub_schoolId"+index).css("font-weight")=="700"){
		return;
	}else{
		var i=0;
		while($("#sub_schoolId"+i).val()!=null){
			if(i==index){change_css_black('sub_schoolId'+i)}
			else{change_css_normal('sub_schoolId'+i);}
			i++;
		}
		$("#submit_schoolId").val(id);
		
		//把关键字搜索输入框置空
		$("#keyworks_input").val("");
		$("#query_string_detail").val("");
		setOtherConditionToDefalut();
		loadData(1);
	}
}
//查询条件选择--选择物品种类
function getDataByGoodsType(id,index){
	if($("#goods_type"+index).css("font-weight")=="700"){
		return;
	}else{
		var i=0;
		while($("#goods_type"+i).val()!=null){
			if(i==index){change_css_black('goods_type'+i)}
			else{change_css_normal('goods_type'+i);}
			i++;
		}
		$("#submit_goodsType").val(id);
		
		//把关键字搜索输入框置空
		$("#keyworks_input").val("");
		$("#query_string_detail").val("");
		setOtherConditionToDefalut();
		loadData(1);
	}
}
//查询条件选择--选择价格
function getDataByPrice(money_from,money_to,index){
	if($("#sub_price"+index).css("font-weight")=="700"){
		return;
	}else{
		var i=0;
		while($("#sub_price"+i).val()!=null){
			if(i==index){change_css_black('sub_price'+i)}
			else{change_css_normal('sub_price'+i);}
			i++;
		}
		//给隐藏域的价格区间赋值
		$("#submit_PriceFrom").val(money_from);
		$("#submit_PriceTo").val(money_to);
		//清空输入框的价格区间
		$("#price_input_from").val("");
		$("#price_input_to").val("");
		
		//把关键字搜索输入框置空
		$("#keyworks_input").val("");
		$("#query_string_detail").val("");
		setOtherConditionToDefalut();
		loadData(1);
	}
}
//挑选价格
function choosePrice(){
	var priceFrom=new String($("#price_input_from").val());
	var priceTo=new String($("#price_input_to").val());
	//整数正则表达式
	var pattern1=/^\d+$/;
	//浮点数正则表达式
	var pattern2=/^(\d+)(\.)(\d+)$/;
	if((pattern1.exec(priceFrom)||pattern2.exec(priceFrom)) && (pattern1.exec(priceTo)||pattern2.exec(priceTo)) ){
		
	}else{
		alert("请填入有效数字");
		return;
	}
	if(new Number(priceFrom)>=new Number(priceTo)){
		alert("价格必须在一个范围内");
		return;
	}
	$("#submit_PriceFrom").val(priceFrom);
	$("#submit_PriceTo").val(priceTo);
	//把超链接都设置成默认（不被选中时的样式）
	change_css_normal("sub_price0");
	change_css_normal("sub_price1");
	change_css_normal("sub_price2");
	change_css_normal("sub_price3");
	change_css_normal("sub_price4");
	change_css_normal("sub_price5");
	change_css_normal("sub_price6");
	change_css_normal("sub_price7");
	change_css_normal("sub_price8");
	change_css_normal("sub_price9");
	//把关键字搜索输入框置空
	$("#keyworks_input").val("");
	$("#query_string_detail").val("");
	//这里load data
	setOtherConditionToDefalut();
	loadData(1);
}
function getDataBySearchDetail(){
	var value=$("#keyworks_input").val();
	if(value==""){
		alert("请输入搜索关键字");
		return;
	}
	//设置隐藏域的值为默认
	setValueToDefault();
	$("#query_string_detail").val(value);
	change_css_black("sub_price0");
	change_css_normal("sub_price1");
	change_css_normal("sub_price2");
	change_css_normal("sub_price3");
	change_css_normal("sub_price4");
	change_css_normal("sub_price5");
	change_css_normal("sub_price6");
	change_css_normal("sub_price7");
	change_css_normal("sub_price8");
	change_css_normal("sub_price9");
	
	change_css_black("buyType_all");
	change_css_normal("buyType_sale");
	change_css_normal("buyType_buy");
	
	setSchoolConditionToNormal();
	
	change_css_black("goods_type0");
	change_css_normal("goods_type1");
	change_css_normal("goods_type2");
	change_css_normal("goods_type3");
	change_css_normal("goods_type4");
	change_css_normal("goods_type5");
	change_css_normal("goods_type6");
	change_css_normal("goods_type7");
	change_css_normal("goods_type8");
	setOtherConditionToDefalut();
	loadData(1);
}
//改变css(粗体、黑色、没有下划线、没有小手)
function change_css_black(id){
	var variable=$("#"+id);
	//"color: blue;text-decoration: underline;cursor: pointer;font-weight: bold;"
	variable.css("color","black");
	variable.css("cursor","auto");
	variable.css("font-weight","bold");
	variable.css("text-decoration","none");
}
/**
 * 改变css(正常字体、蓝色、有下划线、有小手)
 * @param {Object} id
 */
function change_css_normal(id){
	var variable=$("#"+id);
	//"color: blue;text-decoration: underline;cursor: pointer;font-weight: bold;"
	variable.css("color","blue");
	variable.css("cursor","pointer");
	variable.css("font-weight","normal");
	variable.css("text-decoration","underline");
}
/**
 * 
 * @param {Object} page
 * @param {Object} rows
 * @param {Object} total
 */
function generateNewButton(page,rows,total){
	$("#total_info").html("");
	$('<span>当前第&nbsp;<span style="color:red;">'+page+'</span>&nbsp;页,共&nbsp;<span style="color:red;">'+Math.ceil(total/rows)+'</span>&nbsp;页,共&nbsp;<span style="color:red;">'+total+'</span>&nbsp;条信息</span>').appendTo("#total_info");
	if(page!=1){
		$('<span><a style=" margin: 0px 10px;color: blue;text-decoration: underline;cursor: pointer;" onclick="loadData(1)">首页</a>&nbsp;&nbsp</span>').appendTo("#total_info");
	}
	if(page>2){
		$('<span><a style=" margin: 0px 10px;color: blue;text-decoration: underline;cursor: pointer;" onclick="loadData('+(page-1)+')">上一页</a>&nbsp;&nbsp</span>').appendTo("#total_info");
	}
	if(page<(total/rows-1)){
		$('<span><a style=" margin: 0px 10px;color: blue;text-decoration: underline;cursor: pointer;" onclick="loadData('+(page+1)+')">下一页</a>&nbsp;&nbsp</span>').appendTo("#total_info");
	}
	if(page<total/rows){
		$('<span><a style=" margin: 0px 10px;color: blue;text-decoration: underline;cursor: pointer;" onclick="loadData('+Math.ceil(total/rows)+')">尾页</a></span>').appendTo("#total_info");
	}
}
/**
 * 添加学校选择按钮到页面
 */
function add_school(){
	$.post(rootPath+"/json/getCurSchoolByCurCityId.action",
		{},
		function(data){
			$('<span><a id="sub_schoolId0" onclick="getDataBySchoolId(0,0)" style="color: black;text-decoration: none;cursor: auto;font-weight: bold;">全部</a></span>').appendTo("#city_all_school");
			for(var i=0;i<data.list.length;i++){
				$('<span>&nbsp;&nbsp;<a id="sub_schoolId'+(i+1)+'" onclick="getDataBySchoolId('+data.list[i].id+','+(i+1)+');" style="color: blue;text-decoration: underline;cursor: pointer;">'+data.list[i].name+"</a></span>").appendTo("#city_all_school");
			}
		},
		"json"
	);
}
/**
 * 默认加载数据方式--加载当前城市的所有发布信息
 * @param {Object} page
 */
function getDataByCurrCity(page){
	loadData(page);
//	$.post(rootPath+"/json/getGoodsByCurrCity.action",
//		{page:page},
//		function(data){
//			//$('<span>&nbsp;&nbsp;&nbsp;&nbsp;统计信息：&nbsp;&nbsp总共'+data.total+'条信息,当前第'+data.page+'页,每页大小'+data.pageSize+'条</span>').appendTo($("#total_info"));
//			generateNewButton(page,data.pageSize,data.total);
////			for(var i=0;i<data.goods.length;i++){
////				$('<tr><th>标题：'+data.goods[i].title+'</th><td>('+data.goods[i].supplyordemand+')<br>id:'+data.goods[i].id+'<br>价格：'+data.goods[i].price+"(元)<br>详细描述："+data.goods[i].description+"</td></tr>").appendTo("#table_body_info");
////			}
//			withPhoto='';
//			noPhoto='';
//			for(var i=0;i<data.goods.length;i++){
//				withPhoto=withPhoto+'<tr><th><img alt="" style="width: 70px;height: 70px;" src="';
//				if(data.goods[i].photo1!==null && data.goods[i].photo1!=''){
//					withPhoto=withPhoto+''+data.goods[i].photo1;
//				}else{
//					withPhoto=withPhoto+'image/error.jpg';
//				}
//				withPhoto=withPhoto+'"><br>标题：'+data.goods[i].title+'</th>' +
//						  '<td>('+data.goods[i].supplyordemand+')<br>' +
//						  '发布时间:'+data.goods[i].publishTime+'<br>' +
//						  '竞拍类型:'+data.goods[i].isJingpaiOrNot+'<br>' +
//						  '更新时间:'+data.goods[i].updateTime+'<br>' +
//						  '价格：'+data.goods[i].price+"(元)<br>" +
//						  "详细描述："+data.goods[i].description+"<br/>"+
//						  "<a href='acquireGoodsById.action?goodsId="+data.goods[i].id+"'>查看详细</a></td></tr>";
//				noPhoto=noPhoto+'<tr><th>标题：'+data.goods[i].title+'</th>' +
//						'<td>('+data.goods[i].supplyordemand+')<br>' +
//						'发布时间:'+data.goods[i].publishTime+'<br>' +
//						'更新时间:'+data.goods[i].updateTime+'<br>' +
//						'价格：'+data.goods[i].price+"(元)<br>" +
//						"详细描述："+data.goods[i].description+"<br/>"+
//						"<a href='acquireGoodsById.action?goodsId="+data.goods[i].id+"'>查看详细</a></td></tr>";
//				//.appendTo("#table_body_info");
//			}
//			if($("#picture_or_word").val()=='word'){
//				$(noPhoto).appendTo("#table_body_info");
//			}else{
//				$(withPhoto).appendTo("#table_body_info");
//			}
//		},
//		"json"
//	);
}
/**
 * 把隐藏域的值设置为默认
 * 把表单的效果设置为默认
 */
function setValueToDefault(){
	$("#submit_buyType").val("all");
	$("#submit_schoolId").val(0);
	$("#submit_goodsType").val("all");
	$("#submit_PriceFrom").val(0);
	$("#submit_PriceTo").val(0);
	
	
	$("#online_or_all").val("0");
	$("#sort_type").val("mo_ren");
	$("#query_by_time").val("0");
	$("#query_string_detail").val("");
	
	$("#picture_or_word").val("word");
	//输入框的值清空	
	$("#price_input_from").val("");
	$("#price_input_to").val("")
}
function setSchoolConditionToNormal(){
	var id=0;
	var index=0;
	if($("#sub_schoolId"+index).css("font-weight")=="700"){
		return;
	}else{
		var i=0;
		while($("#sub_schoolId"+i).val()!=null){
			if(i==index){change_css_black('sub_schoolId'+i)}
			else{change_css_normal('sub_schoolId'+i);}
			i++;
		}
		$("#submit_schoolId").val(id);
		loadData(1);
	}
}
function registerEvent(){
	$("#online_input").change(function(){
		if($(this).attr("checked")=="checked"){
			$("#online_or_all").val(true);
			loadData(1);
		}else{
			$("#online_or_all").val(0);
			loadData(1);
		}
	});
	$("#sort_select").change(function(){
		if($(this).val()=="1"){
			$("#sort_type").val("by_time");
			loadData(1);
		}else{
			$("#sort_type").val("mo_ren");
			loadData(1);
		}
	});
	$("#query_time_select").change(function(){
		$("#query_by_time").val($(this).val());
		loadData(1);
	});
}
function setOtherConditionToDefalut(){
	$("#photo_input").attr("value","切换到图文");
	$("#picture_or_word").val("word");
	
	$("#sort_select").val(0);
	$("#sort_type").val("mo_ren");
	
	$("#query_by_time").val(0);
	$("#query_time_select").val('0');
}
function eventPhotoOrWords(){
	$("#table_body_info").html("");
	if($("#photo_input").attr("value")=="切换到图文"){
		$("#picture_or_word").val("picture");
		$(withPhoto).appendTo("#table_body_info");
		$("#photo_input").attr("value","切换到文本");
	}else{
		$("#picture_or_word").val("word");
		$(noPhoto).appendTo("#table_body_info");
		$("#photo_input").attr("value","切换到图文");
	}
		
	
	
}